package com.lzh.fastjson.serializer;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;

import java.io.IOException;
import java.lang.reflect.Type;
import java.math.BigDecimal;

/**
 * @author zhehen.lu
 * @date 2025/8/10 14:41
 */
public class FastjsonBigDecimalToStringSerializer implements ObjectSerializer {
    public FastjsonBigDecimalToStringSerializer() {
    }

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        if (object == null) {
            serializer.getWriter().writeNull();
        } else if (!(object instanceof BigDecimal)) {
            serializer.write(object);
        } else {
            BigDecimal bigDecimal = (BigDecimal)object;
            serializer.out.writeString(bigDecimal.toString());
        }
    }
}